﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Routing;
using LifeTree.EntityFrameworkProvider;
using LifeTree.Core.Configuration;
using System.Web.Mvc;
using System.Web;
using LifeTree.Core;
using LifeTree.Mvc.Infrastructure;
using AutoMapper;
using LifeTree.Mvc.ViewModels;
using LifeTree.DomainModel.Entities;

namespace LifeTree.Mvc
{
    public class LifeTreeApplication : HttpApplication
    {
        protected void Application_Start()
        {
            OnStart();
        }

        protected void Application_End()
        {
        }

        protected virtual void OnStart()
        {
            SetupFactory();
            SetupContainer();
            SetupMapper();
            RegisterRoutes(RouteTable.Routes);
            RouteTable.Routes.RouteExistingFiles = true;
        }

        protected virtual void SetupMapper()
        {
            Mapper.CreateMap<Task, TaskViewModel>();
            Mapper.CreateMap<Workbook, WorkBookViewModel>();
            Mapper.CreateMap<TaskViewModel, Task>();
            Mapper.CreateMap<WorkBookViewModel, Workbook>();
        }

        protected virtual void SetupContainer()
        {
            IoC.Initialize();
        }

        protected virtual void SetupFactory()
        {
            ControllerBuilder.Current.SetControllerFactory(GetControllerFactory());
        }

        protected virtual IControllerFactory GetControllerFactory()
        {
            return new LifeTreeControllerFactory();
        }

        protected void RegisterRoutes(RouteCollection routes)
        {
            //Ignore
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("content/{*pathInfo}");
            routes.IgnoreRoute("admin/elmah.axd/{*pathInfo}");

            //Tasks
            routes.MapRoute(
                "CreateWorkBook",
                "workbook/create",
                new { controller = "WorkBook", action = "Create" }
            );

            routes.MapRoute(
                "CreateTask",
                "task/create",
                new { controller = "Task", action = "Create" }
            );

            routes.MapRoute(
                "UpdateTask",
                "task/update",
                new { controller = "Task", action = "Update" }
            );

            routes.MapRoute(
                "DeleteTask",
                "task/delete",
                new { controller = "Task", action = "Delete" }
            );

            routes.MapRoute(
                "ItemTask",
                "task/Item",
                new { controller = "Task", action = "Item" }
            );

            routes.MapRoute(
                "FinishTask",
                "task/finish",
                new { controller = "Task", action = "Finish" }
            );

            routes.MapRoute(
                "ActivateTask",
                "task/activate",
                new { controller = "Task", action = "Activate" }
            );

            routes.MapRoute(
                "CopyTask",
                "task/copy",
                new { controller = "Task", action = "Copy" }
            );

            routes.MapRoute(
                "WorkbookDetails",
                "{workbookName}",
                new { controller = "WorkBook", action = "Details" }
            );


            //Common
            routes.MapRoute("ControllerAction", "{controller}/{action}");

            routes.MapRoute("Default", "", new { controller = "Workbook", action = "Create" });
        }
    }
}
